package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CommUtils;
import com.ruoyi.common.utils.SignUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 * @date 2018-06-25
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice)
    {
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(String ids)
    {
        return noticeMapper.deleteNoticeByIds(Convert.toStrArray(ids));
    }

    public AjaxResult getSerNoticeData(){
        try {
            HttpRequest request = HttpRequest.post(CommUtils.REQ_API_URI+"/SSL/api/v1/getNoticeList");
            //先封装参数得到签名值
            JSONObject paramObj = new JSONObject();
            //得到签名
            String sign = SignUtils.createSign(paramObj, configService.selectConfigByKey("api.appSecret"));
            //赋值必要请求头的值
            request.header("Referer", configService.selectConfigByKey("self.domain"));
            request.header("ssl-sign", sign);   //签名字符串
            request.header("ssl-appid", configService.selectConfigByKey("api.appId"));  //appid
            request.header("ssl-timestamp", String.valueOf(DateUtil.current()));    // 当前时间戳
            //把请求参数赋值到请求中
            request.body(JSONUtil.toJsonStr(paramObj));
            //发起请求
            HttpResponse response = request.execute();
            if(response.getStatus() == 200){
                JSONObject resObj = JSONUtil.parseObj(response.body());
                if(resObj.getInt("code") == 0){
                    List<SysNotice> noticeList = JSONUtil.toList(resObj.getStr("data"), SysNotice.class);
                    return AjaxResult.success("", noticeList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Console.log("同步平台信息失败："+e.getMessage());
        }
        return AjaxResult.error();
    }
}
